package com.dgshanger.sanhxiaofeisc.ui.nearby

import android.content.Intent
import android.support.v4.widget.SwipeRefreshLayout
import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.dgshanger.sanhxiaofeisc.R
import com.dgshanger.sanhxiaofeisc.adapter.CollectibleListAdapter
import com.dgshanger.sanhxiaofeisc.entity.MerchantEntity
import com.dgshanger.sanhxiaofeisc.mvp.contract.CollectibleListContract
import com.dgshanger.sanhxiaofeisc.mvp.presenter.CollectibleListPresenter
import com.dgshanger.sanhxiaofeisc.ui.home.activity.ShopDetailsActivity
import com.xx.baseuilibrary.mvp.lcec.BaseMvpLcecActivity
import kotlinx.android.synthetic.main.activity_collectible_list.*


/**
 * CollectibleListActivity
 * 沉迷学习不能自拔
 * Describe：收藏的商家
 * Created by 雷小星🍀 on 2018/4/18 18:45.
 */
class CollectibleListActivity : BaseMvpLcecActivity<SwipeRefreshLayout,List<MerchantEntity>,CollectibleListContract.Model,CollectibleListContract.View,CollectibleListPresenter>(),CollectibleListContract.View {

    private lateinit var mAdapter : CollectibleListAdapter
    private var currentPage : Int =1

    override fun createPresenter(): CollectibleListPresenter = CollectibleListPresenter()
    override fun getActivityLayoutId(): Int = R.layout.activity_collectible_list

    override fun initData() {
        super.initData()
        initView()
        loadData(true)
    }

    private fun initView() {
        recyclerView.layoutManager=LinearLayoutManager(mContext)
        mAdapter= CollectibleListAdapter(R.layout.item_collectible_list,ArrayList())
        mAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN)
        recyclerView.adapter=mAdapter
        mAdapter.setOnLoadMoreListener {
            loadData(false)
        }
    }

    override fun initEvent() {
        //下拉刷新
        contentView.setOnRefreshListener {
            if (!presenter.isLoading!!){
                currentPage=1
                loadData(true)
            }
        }
        recyclerView.addOnItemTouchListener(object : OnItemClickListener(){
            override fun onSimpleItemClick(adapter: BaseQuickAdapter<*, *>?, view: View?, position: Int) {
                val intent = Intent(mContext, ShopDetailsActivity::class.java)
                intent.putExtra("shop_id", mAdapter!!.data[position].collect_shop_id)
                intent.putExtra("comment_score",mAdapter.data[position].comment_score)
                startActivity(intent)
            }

        })

    }

    override fun loadData(refresh: Boolean) {
        presenter.loadData(refresh,currentPage.toString())
    }

    override fun setData(data: List<MerchantEntity>?) {
        showContent()
        contentView.isRefreshing=false
        currentPage++
        mAdapter.setNewData(data)
    }

    override fun addData(data: MutableList<MerchantEntity>?) {
        currentPage++
        if (data!=null&&!data.isEmpty()){
            mAdapter.loadMoreComplete()
            mAdapter.addData(data)
        }else{
            mAdapter.loadMoreEnd()
        }
    }

    override fun onDataFailure(err: String?) {
        contentView.isRefreshing=false
        mAdapter.loadMoreFail()
    }

}